package cn.kgc.springcloud.service;

import cn.kgc.springcloud.dto.StudentRequestDTO;
import cn.kgc.springcloud.dto.StudentResponseDTO;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.feignclient.CityClient;
import cn.kgc.springcloud.feignclient.ClazzClient;
import cn.kgc.springcloud.feignclient.GroupClient;
import cn.kgc.springcloud.feignclient.TagClient;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/17
 */
@Service
public class StudentServiceImpl implements  StudentService{

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentTagMapper studentTagMapper;

    @Autowired
    private CityClient cityClient;
    @Autowired
    private GroupClient groupClient;
    @Autowired
    private ClazzClient clazzClient;
    @Autowired
    private TagClient tagClient;

    @Override
    public List<StudentResponseDTO> getAll() {
        ArrayList<StudentResponseDTO> studentResponseDTOS = new ArrayList<>();
        List<Student> students = studentMapper.selectAll();
        //遍历集合  查询城市 分组  班级 标签等相关信息
        students.forEach(student -> {
            System.out.println("student = " + student);
            StudentResponseDTO studentResponseDTO = new StudentResponseDTO();
            //对象拷贝  student  ->  StudentResponseDTO
            BeanUtils.copyProperties(student,studentResponseDTO );

            //根据城市id 查询城市信息
            City  city = cityClient.getById(Integer.parseInt(student.getCityId()));
            studentResponseDTO.setCity(city);

            //根据分组的id 查询分组信息
            Group group = groupClient.getById(Integer.parseInt(student.getGroupId()));
            studentResponseDTO.setGroup(group);
            //根据班级id 查询班级信息
            Clazz clazz = clazzClient.getById(Integer.parseInt(student.getClazzId()));
            studentResponseDTO.setClazz(clazz);

            //根据学生id  查询标签列表
            List<StudentTag> studentTags = studentTagMapper.selectById(student.getId());

            studentTags.forEach(studentTag -> {
                //获取标签id  根据标签id 查询标签
                Tag tag = tagClient.getOneById(studentTag.getTid());
                studentResponseDTO.getTags().add(tag);
            });
            //将封装好数据的studentResponseDTO 添加到集合
            studentResponseDTOS.add(studentResponseDTO);
        });

        return studentResponseDTOS;
    }

    @Override
    public StudentResponseDTO addStudent(StudentRequestDTO studentRequestDTO) {

        //将studentRequestDTO  ->student
        Student student = new Student();
        BeanUtils.copyProperties(studentRequestDTO, student);
        //获取生日
        Date bir = studentRequestDTO.getBir();
        //根据生日 计算属相 星座  年龄
        //年龄
        int age = DateUtil.getAge(bir);
        // 星座
        String constellation = DateUtil.getConstellation(bir);
        //生肖
        String attr = DateUtil.getYear(bir);
        student.setAge(age);
        student.setAttr(attr);
        student.setStarts(constellation);

        Integer line = studentMapper.addStudent(student);
        // student 插入完成后 已经包含了自增的id
        long sid = student.getId();
        //根据学生的id 将 tids 添加到学生和标签的关系表中   16 17 18    id：12

        List<Integer> ids = studentRequestDTO.getIds();
        for (Integer tid : ids) {
            studentTagMapper.addSidAndTid(sid,tid);
        }
        return getOne(sid).get(0);
    }


    public List<StudentResponseDTO> getOne(long id) {
        ArrayList<StudentResponseDTO> studentResponseDTOS = new ArrayList<>();
        List<Student> students = studentMapper.selectOne(id);
        //遍历集合  查询城市 分组  班级 标签等相关信息
        students.forEach(student -> {
            System.out.println("student = " + student);
            StudentResponseDTO studentResponseDTO = new StudentResponseDTO();
            //对象拷贝  student  ->  StudentResponseDTO
            BeanUtils.copyProperties(student,studentResponseDTO );

            //根据城市id 查询城市信息
            City  city = cityClient.getById(Integer.parseInt(student.getCityId()));
            studentResponseDTO.setCity(city);

            //根据分组的id 查询分组信息
            Group group = groupClient.getById(Integer.parseInt(student.getGroupId()));
            studentResponseDTO.setGroup(group);
            //根据班级id 查询班级信息
            Clazz clazz = clazzClient.getById(Integer.parseInt(student.getClazzId()));
            studentResponseDTO.setClazz(clazz);

            //根据学生id  查询标签列表
            List<StudentTag> studentTags = studentTagMapper.selectById(student.getId());

            studentTags.forEach(studentTag -> {
                //获取标签id  根据标签id 查询标签
                Tag tag = tagClient.getOneById(studentTag.getTid());
                studentResponseDTO.getTags().add(tag);
            });
            //将封装好数据的studentResponseDTO 添加到集合
            studentResponseDTOS.add(studentResponseDTO);
        });

        return studentResponseDTOS;
    }
}
